package com.ny.demo517;

import com.ny.demo527.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDAO {
    public boolean addEmp(Emp emp) {
        String sql = "INSERT INTO employees(number, name, age) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, emp.getNumber());
            pstmt.setString(2, emp.getName());
            pstmt.setInt(3, emp.getAge());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Emp> findAllEmp() {
        List<Emp> list = new ArrayList<>();
        String sql = "SELECT * FROM employees";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                Emp emp = new Emp();
                emp.setNumber(rs.getString("number"));
                emp.setName(rs.getString("name"));
                emp.setAge(rs.getInt("age"));
                list.add(emp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    // 实现其他CRUD方法...
}
